var loginReq = Titanium.Network.createHTTPClient();

/**
 * Facebook application data
 */
var facebook = Alloy.Globals.Facebook;
facebook.appid = '781948988496907';
facebook.permissions = ['user_about_me', 'user_birthday', 'email'];
//facebook.forceDialogAuth = true;

$.fbButton.style = facebook.BUTTON_STYLE_WIDE;
$.fbButton.addEventListener('login', function(e) {
	alert('xpto');
	if (e.success) {
		alert('Logged In');
	} else if (e.error) {
		alert(e.error);
	} else if (e.cancelled) {
		alert("Canceled");
	}
});

facebook.addEventListener('login', function(e) {
	if (e.success) {
		var userData = {
			utilizador : e.data.id,
			password : Ti.Utils.md5HexDigest('facebook' + e.data.id),
			email : e.data.email,
			primeiroNome : e.data.first_name,
			ultimoNome : e.data.last_name,
			dataNascimento : new Date(e.data.birthday),

		};

		var xhr = Ti.Network.createHTTPClient({
			onload : function() {
				var json = this.responseText;
				var response = JSON.parse(json);
				if (response.autenticado == 1) {
					Ti.App.Properties.setObject('autenticado', userData);
					closeWindow();
				} else {
					alert(response.mensagem);
				}
			},
			onerror : function(e) {
				Ti.API.debug(e.error);
				alert('error');
			},
			timeout : 100000 // in milliseconds
		});
		xhr.open('POST', ip + "/server/rest/servico-autenticacao/facebook");
		xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
		//Define o encoding.
		xhr.send(JSON.stringify(userData));

	} else if (e.error) {
		alert(e.error);
	} else if (e.cancelled) {
		alert("Canceled");
	} else {
		alert('xpto');
	}
});

function autenticar() {

	if ($.itemField.value != '' && $.passwordField.value != '') {
		var args = {
			utilizador : $.itemField.value,
			password : Ti.Utils.md5HexDigest($.passwordField.value)
		};

		var xhr = Ti.Network.createHTTPClient({
			onload : function() {
				var json = this.responseText;
				var response = JSON.parse(json);
				if (response.autenticado == 1) {
					Ti.App.Properties.setObject('autenticado', args);
					alert("Welcome " + response.name + ". Your email is: " + response.email);
					console.log(Ti.App.Properties.getObject('autenticado', null));
					closeWindow();
				} else {
					alert(response.mensagem);
				}
			},
			onerror : function(e) {
				Ti.API.debug(e.error);
				alert('error');
			},
			timeout : 100000 // in milliseconds
		});
		xhr.open('POST', ip + "/server/rest/servico-autenticacao/autenticar");
		xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
		//Define o encoding.
		xhr.send(JSON.stringify(args));

	} else {
		alert("Username/Password are required");
	}
}

function registar(event) {
	/*
	 * Exportar o navigationWindow
	 */
	Alloy.Globals.login = $.login;
	
	var roomview = Alloy.createController("registar").getView();
	roomview.open();
}

function fbAutenticar(event) {
	console.log("entry");
}

function focusTextField() {
	$.itemField.focus();
}

function closeKeyboard(e) {
	e.source.blur();
}

function closeWindow() {
	$.login.close();
}